{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<img src=\"https://deecamp.chuangxin.com/assets/image/logo_nav_zh.jpg\" width=\"40%\">"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import csv"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "block_1.attributes  block_2.attributes\tblock_3.attributes\r\n",
      "block_1.tuples\t    block_2.tuples\tblock_3.tuples\r\n"
     ]
    }
   ],
   "source": [
    "! ls /data/jupyter_root/ZJ/test1.0 "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 定义一个函数，先输出原始feature文件的P & R，再输出通过D-Cube分块的P & R"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "名称 | 定义\n",
    " --- | ---\n",
    "TP\t| 真实类别为positive，模型预测的类别也为positive\n",
    "FP\t| 预测为positive，但真实类别为negative，真实类别和预测类别不一致\n",
    "FN\t| 预测为negative，但真实类别为positive，真实类别和预测类别不一致\n",
    "TN\t| 真实类别为negative，模型预测的类别也为negative"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "准确率（accuracy）计算公式如下所示 不计算这个\n",
    "\n",
    "\\begin{equation}\\label{equ:accuracy} \\mbox{accuracy} = \\frac{TP+TN}{TP+TN+FP+FN} = \\frac{TP+TN }{\\mbox{all data}} \\end{equation}\n",
    "\n",
    "positive class的精确率（precision）计算公式如下\n",
    "\n",
    "\\begin{equation}\\label{equ:precision} \\mbox{precision} = \\frac{TP}{TP+FP} = \\frac{TP}{\\mbox{预测为positive的样本}} \\end{equation}\n",
    "\n",
    "positive class的召回率（recall）计算公式如下\n",
    "\n",
    "\\begin{equation}\\label{equ:recall} \\mbox{recall} = \\frac{TP}{TP+FN} = \\frac{TP}{\\mbox{真实为positive的样本}} \\end{equation}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "ename": "SyntaxError",
     "evalue": "invalid syntax (<ipython-input-13-3ac69efef015>, line 18)",
     "output_type": "error",
     "traceback": [
      "\u001b[0;36m  File \u001b[0;32m\"<ipython-input-13-3ac69efef015>\"\u001b[0;36m, line \u001b[0;32m18\u001b[0m\n\u001b[0;31m    result=pd.read_csv(/data/jupyter_root/dcube_data/+str(f_count)+\".txt\",sep=',',names=user_index)#读取全部的数据，并给上names\u001b[0m\n\u001b[0m                       ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m invalid syntax\n"
     ]
    }
   ],
   "source": [
    "def get_PR(f_count):\n",
    "    \n",
    "    #准备用于读取数据的 pandas头（防止第一行数据成为标签）\n",
    "    data_path=\"/data/jupyter_root/dcube_data/feature\"#feature文件位置\n",
    "    try:\n",
    "        cols = pd.read_csv(data_path+str(f_count)+\"/test1.0/blocks.txt\", nrows=1).columns#先读第一行，用于提取数据维度\n",
    "    except:\n",
    "        return 0\n",
    "    user_index=['user_name']#初始化一个等待补全的names\n",
    "    for i in range(1,cols.shape[0]-1):\n",
    "            user_index.append(str(i))\n",
    "    user_index.append('count')#补上最后的count\n",
    "    \n",
    "    #开始读取数据\n",
    "    result=pd.read_csv(data_path+str(f_count)+\"/test1.0/blocks.txt\",sep=',',names=user_index)#读取全部的数据，并给上names\n",
    "    names = result['user_name'].drop_duplicates()#预测出坏用户的用户列表\n",
    "    \n",
    "    result=pd.read_csv(/data/jupyter_root/dcube_data/+str(f_count)+\".txt\",sep=',',names=user_index)#读取全部的数据，并给上names\n",
    "    names = result['user_name'].drop_duplicates()#预测出坏用户的用户列表\n",
    "\n",
    "    dataset = pd.read_csv(open('/data/csv/label_eventsV1.csv','r',encoding = 'gb18030'))#读取总标签数据\n",
    "    data = dataset[dataset['user_name'].isin(names)][['user_name','label']]#预测出坏用户的真实标签分布\n",
    "    data = data.drop_duplicates()#去重\n",
    "    Precision = data.label.sum()/data.label.count()  #精确率计算\n",
    "\n",
    "    data_label_1 = dataset[dataset['label']==1][['user_name','label']] #原数据中label为1的坏用户\n",
    "    data_label_1 = data_label_1.drop_duplicates()#去重\n",
    "    Recall = data.label.sum()/data_label_1.label.count() # 召回率计算\n",
    "    print('测试特征'+str(f_count))\n",
    "    print('\\t\\t精确率 Precision\\t= '+str(Precision))\n",
    "    print('\\t\\t召回率 Recall   \\t= '+str(Recall))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 精确率Precision的计算"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 召回率Recall的计算"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "data_label_1 = dataset[dataset['label']==1][['user_name','label']] #原数据中label为1的坏用户\n",
    "data_label_1 = data_label_1.drop_duplicates()#去重\n",
    "Recall = data.label.sum()/data_label_1.label.count() # 召回率计算\n",
    "print(Recall)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/opt/anaconda3/lib/python3.7/site-packages/IPython/core/interactiveshell.py:3249: DtypeWarning: Columns (4,11) have mixed types. Specify dtype option on import or set low_memory=False.\n",
      "  if (await self.run_code(code, result,  async_=asy)):\n",
      "/opt/anaconda3/lib/python3.7/site-packages/ipykernel_launcher.py:13: RuntimeWarning: invalid value encountered in long_scalars\n",
      "  del sys.path[0]\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "测试特征1\n",
      "\t\t精确率 Precision\t= nan\n",
      "\t\t召回率 Recall   \t= 0.0\n",
      "测试特征2\n",
      "\t\t精确率 Precision\t= nan\n",
      "\t\t召回率 Recall   \t= 0.0\n",
      "测试特征3\n",
      "\t\t精确率 Precision\t= nan\n",
      "\t\t召回率 Recall   \t= 0.0\n",
      "测试特征4\n",
      "\t\t精确率 Precision\t= nan\n",
      "\t\t召回率 Recall   \t= 0.0\n",
      "测试特征5\n",
      "\t\t精确率 Precision\t= nan\n",
      "\t\t召回率 Recall   \t= 0.0\n",
      "测试特征6\n",
      "\t\t精确率 Precision\t= nan\n",
      "\t\t召回率 Recall   \t= 0.0\n",
      "测试特征7\n",
      "\t\t精确率 Precision\t= 1.0\n",
      "\t\t召回率 Recall   \t= 0.00018057060310581438\n",
      "测试特征8\n",
      "\t\t精确率 Precision\t= nan\n",
      "\t\t召回率 Recall   \t= 0.0\n",
      "测试特征9\n",
      "\t\t精确率 Precision\t= nan\n",
      "\t\t召回率 Recall   \t= 0.0\n",
      "测试特征10\n",
      "\t\t精确率 Precision\t= nan\n",
      "\t\t召回率 Recall   \t= 0.0\n",
      "测试特征11\n",
      "\t\t精确率 Precision\t= nan\n",
      "\t\t召回率 Recall   \t= 0.0\n",
      "测试特征12\n",
      "\t\t精确率 Precision\t= nan\n",
      "\t\t召回率 Recall   \t= 0.0\n",
      "测试特征13\n",
      "\t\t精确率 Precision\t= nan\n",
      "\t\t召回率 Recall   \t= 0.0\n",
      "测试特征14\n",
      "\t\t精确率 Precision\t= nan\n",
      "\t\t召回率 Recall   \t= 0.0\n",
      "测试特征15\n",
      "\t\t精确率 Precision\t= 1.0\n",
      "\t\t召回率 Recall   \t= 0.00018057060310581438\n",
      "测试特征16\n",
      "\t\t精确率 Precision\t= 1.0\n",
      "\t\t召回率 Recall   \t= 0.00018057060310581438\n",
      "测试特征17\n",
      "\t\t精确率 Precision\t= 1.0\n",
      "\t\t召回率 Recall   \t= 0.00018057060310581438\n",
      "测试特征18\n",
      "\t\t精确率 Precision\t= nan\n",
      "\t\t召回率 Recall   \t= 0.0\n",
      "测试特征19\n",
      "\t\t精确率 Precision\t= nan\n",
      "\t\t召回率 Recall   \t= 0.0\n",
      "测试特征20\n",
      "\t\t精确率 Precision\t= 1.0\n",
      "\t\t召回率 Recall   \t= 0.00018057060310581438\n",
      "测试特征21\n",
      "\t\t精确率 Precision\t= 0.10204081632653061\n",
      "\t\t召回率 Recall   \t= 0.0009028530155290719\n",
      "测试特征22\n",
      "\t\t精确率 Precision\t= nan\n",
      "\t\t召回率 Recall   \t= 0.0\n",
      "测试特征23\n",
      "\t\t精确率 Precision\t= nan\n",
      "\t\t召回率 Recall   \t= 0.0\n",
      "测试特征24\n",
      "\t\t精确率 Precision\t= nan\n",
      "\t\t召回率 Recall   \t= 0.0\n",
      "测试特征25\n",
      "\t\t精确率 Precision\t= nan\n",
      "\t\t召回率 Recall   \t= 0.0\n",
      "测试特征26\n",
      "\t\t精确率 Precision\t= nan\n",
      "\t\t召回率 Recall   \t= 0.0\n",
      "测试特征27\n",
      "\t\t精确率 Precision\t= 0.0\n",
      "\t\t召回率 Recall   \t= 0.0\n",
      "测试特征28\n",
      "\t\t精确率 Precision\t= 0.0\n",
      "\t\t召回率 Recall   \t= 0.0\n",
      "测试特征29\n",
      "\t\t精确率 Precision\t= 0.0\n",
      "\t\t召回率 Recall   \t= 0.0\n",
      "测试特征30\n",
      "\t\t精确率 Precision\t= 0.0\n",
      "\t\t召回率 Recall   \t= 0.0\n",
      "测试特征31\n",
      "\t\t精确率 Precision\t= 0.0\n",
      "\t\t召回率 Recall   \t= 0.0\n",
      "测试特征32\n",
      "\t\t精确率 Precision\t= 0.2\n",
      "\t\t召回率 Recall   \t= 0.00018057060310581438\n",
      "测试特征33\n",
      "\t\t精确率 Precision\t= 0.23837209302325582\n",
      "\t\t召回率 Recall   \t= 0.007403394727338389\n",
      "测试特征34\n",
      "\t\t精确率 Precision\t= 0.0\n",
      "\t\t召回率 Recall   \t= 0.0\n",
      "测试特征35\n",
      "\t\t精确率 Precision\t= 0.582824668490844\n",
      "\t\t召回率 Recall   \t= 1.0\n",
      "测试特征36\n",
      "\t\t精确率 Precision\t= 0.0\n",
      "\t\t召回率 Recall   \t= 0.0\n",
      "测试特征37\n",
      "\t\t精确率 Precision\t= 0.057692307692307696\n",
      "\t\t召回率 Recall   \t= 0.0005417118093174431\n",
      "测试特征38\n",
      "\t\t精确率 Precision\t= 0.6666666666666666\n",
      "\t\t召回率 Recall   \t= 0.00036114120621162876\n",
      "测试特征39\n",
      "\t\t精确率 Precision\t= 0.4\n",
      "\t\t召回率 Recall   \t= 0.00036114120621162876\n",
      "测试特征40\n",
      "\t\t精确率 Precision\t= 0.59366391184573\n",
      "\t\t召回率 Recall   \t= 0.233477789815818\n",
      "测试特征41\n",
      "\t\t精确率 Precision\t= 0.4497991967871486\n",
      "\t\t召回率 Recall   \t= 0.040447815095702416\n",
      "测试特征42\n",
      "\t\t精确率 Precision\t= 0.3387096774193548\n",
      "\t\t召回率 Recall   \t= 0.011375947995666305\n",
      "测试特征43\n",
      "\t\t精确率 Precision\t= 0.0\n",
      "\t\t召回率 Recall   \t= 0.0\n",
      "测试特征44\n",
      "\t\t精确率 Precision\t= 0.6135916877281662\n",
      "\t\t召回率 Recall   \t= 0.3945467677862044\n",
      "测试特征45\n",
      "\t\t精确率 Precision\t= 0.5862911103874959\n",
      "\t\t召回率 Recall   \t= 0.6502347417840375\n",
      "测试特征46\n",
      "\t\t精确率 Precision\t= 0.5857058476077968\n",
      "\t\t召回率 Recall   \t= 0.8952690501986277\n",
      "测试特征47\n",
      "\t\t精确率 Precision\t= 0.5523385300668151\n",
      "\t\t召回率 Recall   \t= 0.04478150957024196\n",
      "测试特征48\n",
      "\t\t精确率 Precision\t= 0.0\n",
      "\t\t召回率 Recall   \t= 0.0\n",
      "测试特征49\n",
      "\t\t精确率 Precision\t= 0.13541666666666666\n",
      "\t\t召回率 Recall   \t= 0.002347417840375587\n",
      "测试特征50\n",
      "\t\t精确率 Precision\t= 0.17272727272727273\n",
      "\t\t召回率 Recall   \t= 0.0034308414590104733\n",
      "测试特征51\n",
      "\t\t精确率 Precision\t= 0.6610333172380493\n",
      "\t\t召回率 Recall   \t= 0.49440231130371975\n",
      "测试特征52\n",
      "\t\t精确率 Precision\t= 0.6678383128295254\n",
      "\t\t召回率 Recall   \t= 0.48031780426146625\n",
      "测试特征53\n",
      "\t\t精确率 Precision\t= 0.6353784860557768\n",
      "\t\t召回率 Recall   \t= 0.7199349945828819\n",
      "测试特征54\n",
      "\t\t精确率 Precision\t= 0.6311977715877437\n",
      "\t\t召回率 Recall   \t= 0.40917298663777535\n",
      "测试特征55\n",
      "\t\t精确率 Precision\t= 0.5892734984265974\n",
      "\t\t召回率 Recall   \t= 0.7777175875767425\n",
      "测试特征56\n",
      "\t\t精确率 Precision\t= 0.5888767720828789\n",
      "\t\t召回率 Recall   \t= 0.7800650054171181\n",
      "测试特征57\n",
      "\t\t精确率 Precision\t= 0.5887533875338753\n",
      "\t\t召回率 Recall   \t= 0.7845792704947635\n",
      "测试特征58\n",
      "\t\t精确率 Precision\t= 0.5893246187363834\n",
      "\t\t召回率 Recall   \t= 0.7815095702419647\n",
      "测试特征59\n",
      "\t\t精确率 Precision\t= 0.5925438596491228\n",
      "\t\t召回率 Recall   \t= 0.9758035391838209\n",
      "测试特征60\n",
      "\t\t精确率 Precision\t= 0.5977061242155378\n",
      "\t\t召回率 Recall   \t= 0.9974720115565185\n",
      "测试特征61\n",
      "\t\t精确率 Precision\t= 0.5842388437598903\n",
      "\t\t召回率 Recall   \t= 1.0\n",
      "测试特征62\n",
      "\t\t精确率 Precision\t= 0.600864553314121\n",
      "\t\t召回率 Recall   \t= 0.07529794149512459\n",
      "测试特征63\n",
      "\t\t精确率 Precision\t= 0.0\n",
      "\t\t召回率 Recall   \t= 0.0\n",
      "测试特征64\n",
      "\t\t精确率 Precision\t= 0.0\n",
      "\t\t召回率 Recall   \t= 0.0\n",
      "测试特征65\n",
      "\t\t精确率 Precision\t= 0.21621621621621623\n",
      "\t\t召回率 Recall   \t= 0.00577825929938606\n",
      "测试特征66\n",
      "\t\t精确率 Precision\t= 0.6465540724598202\n",
      "\t\t召回率 Recall   \t= 0.8571686529433008\n",
      "测试特征67\n",
      "\t\t精确率 Precision\t= 0.6566400962550759\n",
      "\t\t召回率 Recall   \t= 0.7883712531599856\n",
      "测试特征68\n",
      "\t\t精确率 Precision\t= 0.5895166780122533\n",
      "\t\t召回率 Recall   \t= 0.7818707114481762\n",
      "测试特征69\n",
      "\t\t精确率 Precision\t= 0.5887533875338753\n",
      "\t\t召回率 Recall   \t= 0.7845792704947635\n",
      "测试特征70\n",
      "\t\t精确率 Precision\t= 0.5887533875338753\n",
      "\t\t召回率 Recall   \t= 0.7845792704947635\n",
      "测试特征71\n",
      "\t\t精确率 Precision\t= 0.5887533875338753\n",
      "\t\t召回率 Recall   \t= 0.7845792704947635\n",
      "测试特征72\n",
      "\t\t精确率 Precision\t= 0.5939586297471818\n",
      "\t\t召回率 Recall   \t= 0.9799566630552546\n",
      "测试特征73\n",
      "\t\t精确率 Precision\t= 0.5933641146784066\n",
      "\t\t召回率 Recall   \t= 0.9978331527627302\n",
      "测试特征74\n",
      "\t\t精确率 Precision\t= 0.5890236119974473\n",
      "\t\t召回率 Recall   \t= 1.0\n",
      "测试特征75\n",
      "\t\t精确率 Precision\t= 0.6479481641468683\n",
      "\t\t召回率 Recall   \t= 0.866738894907909\n",
      "测试特征76\n",
      "\t\t精确率 Precision\t= 0.0\n",
      "\t\t召回率 Recall   \t= 0.0\n",
      "测试特征77\n",
      "\t\t精确率 Precision\t= 0.2\n",
      "\t\t召回率 Recall   \t= 0.004875406283856988\n",
      "测试特征78\n",
      "\t\t精确率 Precision\t= 0.32113821138211385\n",
      "\t\t召回率 Recall   \t= 0.014265077645359335\n",
      "测试特征79\n",
      "\t\t精确率 Precision\t= 0.6481053918239847\n",
      "\t\t召回率 Recall   \t= 0.8616829180209462\n",
      "测试特征80\n",
      "\t\t精确率 Precision\t= 0.6500068092060466\n",
      "\t\t召回率 Recall   \t= 0.861863488624052\n"
     ]
    }
   ],
   "source": [
    "for feature_count in range(1,81):\n",
    "    get_PR(feature_count)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.8"
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "position": {
    "height": "389.9px",
    "left": "1090.64px",
    "right": "20px",
    "top": "116.988px",
    "width": "454.025px"
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
